//
//  YGScienceArticleDetailViewController.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/5/7.
//

#import "YGScienceArticleDetailViewController.h"
#import "YGArticleBLL.h"

@interface YGScienceArticleDetailViewController ()<UIWebViewDelegate>
@property (nonatomic, strong) UILabel *themeLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation YGScienceArticleDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : UIColorFromRGB(0x333333)}];
    [self setTitle:@"营养科普详情"];
    [self initView];
    [self initconstraints];
    [self requestData];
    // Do any additional setup after loading the view.
}

-(void)initView{
    
    [self.view addSubview:self.themeLabel];
    [self.view addSubview:self.dateLabel];
    
}

-(void)initconstraints{
    [self.themeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.view.mas_top);
            make.left.mas_equalTo(self.view.mas_left).offset(10);
            make.width.mas_equalTo(self.view);
            make.height.mas_equalTo(30);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.themeLabel.mas_bottom).offset(10);
            make.left.mas_equalTo(self.themeLabel.mas_left);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(14);
    }];
    
}

-(void)requestData{
    WeakSelf(self)
    [YGArticleBLL requestScienceCenterArticleDetailWithId:self.id Success:^(YGScienceCenterArticleDetailModel * _Nonnull model) {
        weakself.themeLabel.text = [model.theme stringByRemovingPercentEncoding];
        weakself.dateLabel.text = [self time_timestampToString:[[model valueForKey:@"createTime"]integerValue]];
        [weakself.webView loadHTMLString:model.content baseURL:nil];
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (NSString *)time_timestampToString:(NSInteger)timestamp{

    timestamp = timestamp/1000.0;
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];

    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];

     [dateFormat setDateFormat:@"yyyy-MM-dd"];

    NSString *string=[dateFormat stringFromDate:confromTimesp];

    return string;

}

#pragma setter

- (UILabel *)themeLabel{
    if (!_themeLabel) {
        _themeLabel = [[UILabel alloc]init];
        _themeLabel.textColor = UIColorFromRGB(0x333333);
        _themeLabel.font = [UIFont systemFontOfSize:14];
        _themeLabel.numberOfLines = 0;
    }
    return _themeLabel;
}

- (UILabel *)dateLabel{
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc]init];
        _dateLabel.textColor = UIColorFromRGB(0x666666);
        _dateLabel.font = [UIFont systemFontOfSize:12];
    }
    return _dateLabel;
}

- (UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, self.dateLabel.bottom, self.view.width, self.view.height - self.dateLabel.bottom)];
        _webView.delegate = self;
        [self.view addSubview:_webView];
    }
    return _webView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
